/*
 ============================================================================
 Name		: ResponseDispatcher.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   :
 Description : CResponseDispatcher implementation
 ============================================================================
 */

#include "ResponseBanks.h"
#include "ResponseBanksData.h"
#include "ResponseDefinses.h"
#include "ProjectsDefines.h"
#include "BetterUserHBufC.h"

CResponseBanks::CResponseBanks( )
{
    // No implementation required
}

CResponseBanks::~CResponseBanks( )
{
    SAFE_DELETE( _data );
}

CResponseBanks* CResponseBanks::NewLC( )
{
    CResponseBanks* self = new ( ELeave ) CResponseBanks( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

CResponseBanks* CResponseBanks::NewL( )
{
    CResponseBanks* self = CResponseBanks::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void CResponseBanks::ConstructL( )
{
    _data = CResponseBanksData::NewL( );
}

void CResponseBanks::LoadValidResponseFromXmlNode( CElement* elementResponse )
{
    _ASSERT_LOG( elementResponse != NULL );

    if( !elementResponse )
    {
        return;
    }

    CElement* elementResult = elementResponse->GetElement( kResponseResultNode );

    _ASSERT_LOG( elementResult != NULL );
    if( !elementResult )
    {
        return;
    }
    _LIT( kResponseBanksNode, "banks");

    CElement* elementBanks = elementResult->GetElement( kResponseBanksNode );

    _ASSERT_LOG( elementBanks != NULL );
    if( !elementBanks )
    {
        return;
    }

    int count = elementBanks->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* elementItem = elementBanks->GetElement( index );
        _LIT( kResponseItemNode, "item" );
        if( elementItem && elementItem->GetName( ) == kResponseItemNode )
        {
            CResponseBankItemData* bankItem = CResponseBankItemData::NewLC( );
            _LIT( kResponseIDNode, "id");

            bankItem->_id = BetterUtility::GetElementText( elementItem->GetElement( kResponseIDNode ) );

            bankItem->_name = BetterUtility::GetElementText( elementItem->GetElement( kResponseNameNode ) );

            CleanupStack::Pop( ); // bankItem;
            _data->_banks.Append( bankItem );
        }
    }
}
